<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="直播id" prop="exportId">
        <el-input
          v-model="queryParams.exportId"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="追踪ID，报bug带" prop="traceId">
        <el-input
          v-model="queryParams.traceId"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="观看人数" prop="newWatchUv">
        <el-input
          v-model="queryParams.newWatchUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="最大在线人数" prop="maxOnlineWatchUv">
        <el-input
          v-model="queryParams.maxOnlineWatchUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="曝光人数" prop="impressionUv">
        <el-input
          v-model="queryParams.impressionUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平均观看时长（秒）" prop="averageWatchSecondsPerAudience">
        <el-input
          v-model="queryParams.averageWatchSecondsPerAudience"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="新增关注人数" prop="newFollowUv">
        <el-input
          v-model="queryParams.newFollowUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="新增粉丝团人数" prop="newFansClubUv">
        <el-input
          v-model="queryParams.newFansClubUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="评论人数" prop="commentUv">
        <el-input
          v-model="queryParams.commentUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="打赏人数" prop="rewardUv">
        <el-input
          v-model="queryParams.rewardUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="分享直播间人数" prop="sharingUv">
        <el-input
          v-model="queryParams.sharingUv"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="热度" prop="hotQuota">
        <el-input
          v-model="queryParams.hotQuota"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="直播大屏基础数据json" prop="liveDashboardData">
        <el-input
          v-model="queryParams.liveDashboardData"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内容力数据json" prop="liveComparisonIndex">
        <el-input
          v-model="queryParams.liveComparisonIndex"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电商数据概要数据json" prop="liveEcDataSummary">
        <el-input
          v-model="queryParams.liveEcDataSummary"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电商转化力数据json" prop="liveEcConversionMetric">
        <el-input
          v-model="queryParams.liveEcConversionMetric"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电商渠道分布json" prop="liveDistributionChannel">
        <el-input
          v-model="queryParams.liveDistributionChannel"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电商商品数据json" prop="singleLiveEcSpuDataPageV2">
        <el-input
          v-model="queryParams.singleLiveEcSpuDataPageV2"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="直播持续时间" prop="liveDuration">
        <el-input
          v-model="queryParams.liveDuration"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="直播开始时间" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['integration:weixinvideo-livedata:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['integration:weixinvideo-livedata:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="直播id" align="center" prop="exportId" />
      <el-table-column label="追踪ID，报bug带" align="center" prop="traceId" />
      <el-table-column label="观看人数" align="center" prop="newWatchUv" />
      <el-table-column label="最大在线人数" align="center" prop="maxOnlineWatchUv" />
      <el-table-column label="曝光人数" align="center" prop="impressionUv" />
      <el-table-column label="平均观看时长（秒）" align="center" prop="averageWatchSecondsPerAudience" />
      <el-table-column label="新增关注人数" align="center" prop="newFollowUv" />
      <el-table-column label="新增粉丝团人数" align="center" prop="newFansClubUv" />
      <el-table-column label="评论人数" align="center" prop="commentUv" />
      <el-table-column label="打赏人数" align="center" prop="rewardUv" />
      <el-table-column label="分享直播间人数" align="center" prop="sharingUv" />
      <el-table-column label="热度" align="center" prop="hotQuota" />
      <el-table-column label="直播大屏基础数据json" align="center" prop="liveDashboardData" />
      <el-table-column label="内容力数据json" align="center" prop="liveComparisonIndex" />
      <el-table-column label="电商数据概要数据json" align="center" prop="liveEcDataSummary" />
      <el-table-column label="电商转化力数据json" align="center" prop="liveEcConversionMetric" />
      <el-table-column label="电商画像数据json" align="center" prop="liveEcProfile" />
      <el-table-column label="电商渠道分布json" align="center" prop="liveDistributionChannel" />
      <el-table-column label="电商商品数据json" align="center" prop="singleLiveEcSpuDataPageV2" />
      <el-table-column label="直播持续时间" align="center" prop="liveDuration" />
      <el-table-column label="直播开始时间" align="center" prop="startTime" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['integration:weixinvideo-livedata:update']"
          >
            编辑
          </el-button>
          <el-button
              link
              type="primary"
              @click="openForm('detail', scope.row.id)"
              v-hasPermi="['integration:weixinvideo-livedata:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['integration:weixinvideo-livedata:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WeixinvideoLivedataForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WeixinvideoLivedataApi, WeixinvideoLivedataVO } from '@/api/integration/weixinvideolivedata'
import WeixinvideoLivedataForm from './WeixinvideoLivedataForm.vue'

/** 微信视频号直播大屏数据 列表 */
defineOptions({ name: 'WeixinvideoLivedata' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WeixinvideoLivedataVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  exportId: undefined,
  traceId: undefined,
  newWatchUv: undefined,
  maxOnlineWatchUv: undefined,
  impressionUv: undefined,
  averageWatchSecondsPerAudience: undefined,
  newFollowUv: undefined,
  newFansClubUv: undefined,
  commentUv: undefined,
  rewardUv: undefined,
  sharingUv: undefined,
  hotQuota: undefined,
  liveDashboardData: undefined,
  liveComparisonIndex: undefined,
  liveEcDataSummary: undefined,
  liveEcConversionMetric: undefined,
  liveEcProfile: undefined,
  liveDistributionChannel: undefined,
  singleLiveEcSpuDataPageV2: undefined,
  liveDuration: undefined,
  startTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WeixinvideoLivedataApi.getWeixinvideoLivedataPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WeixinvideoLivedataApi.deleteWeixinvideoLivedata(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WeixinvideoLivedataApi.exportWeixinvideoLivedata(queryParams)
    download.excel(data, '微信视频号直播大屏数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>